package com.lineage.server.timecontroller.pc;

import com.lineage.server.datatables.lock.UpdateLocReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_Teleport;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.PcOtherThreadPool;
import com.lineage.server.world.World;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import java.util.TimerTask;

public class UnfreezingTimer extends TimerTask {
  private static Random _random = new Random();
  
  public void start() {
    int timeMillis = 1000;
    PcOtherThreadPool.get().scheduleAtFixedRate(this, 1000L, 1000L);
  }
  
  public void run() {
    Collection<L1PcInstance> all = World.get().getAllPlayers();
    if (all.isEmpty())
      return; 
    Iterator<L1PcInstance> iter = all.iterator();
    while (iter.hasNext()) {
      L1PcInstance tgpc = iter.next();
      if (tgpc.get_unfreezingTime() != 0) {
        int time = tgpc.get_unfreezingTime() - 1;
        tgpc.set_unfreezingTime(time);
        if (time <= 0) {
          UpdateLocReading.get().setPcLoc(tgpc.getAccountName());
          tgpc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR帐号内所有人物传送回指定位置！"));
          L1Teleport.teleport(tgpc, 33443, 32797, (short)4, 5, true);
        } else {
          tgpc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR" + time + "秒后传送回指定位置！"));
        } 
      } 
      if (tgpc.get_misslocTime() != 0) {
        int time = tgpc.get_misslocTime() - 1;
        tgpc.set_misslocTime(time);
        if (time <= 0) {
          tgpc.setTeleportX(tgpc.getX() + _random.nextInt(2));
          tgpc.setTeleportY(tgpc.getY() + _random.nextInt(2));
          tgpc.setTeleportMapId(tgpc.getMapId());
          tgpc.setTeleportHeading(5);
          tgpc.sendPackets((ServerBasePacket)new S_Teleport(tgpc));
          tgpc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR解除人物错位！"));
          continue;
        } 
        tgpc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR" + time + "秒后解除人物错位！"));
      } 
    } 
  }
}
